package com.blackline.UI.page;

import java.util.Iterator;
import java.util.Set;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;
import org.apache.log4j.Logger;
import com.blackline.declareUI.UI;

/**
 * @author XBO
 * @version Jan, 2013
 * <p>This page is for the common functions for all the pages like VerifyPageTile().
 */
public class page {
	private static Logger logger=Logger.getLogger(page.class);
	  
	UI UI = new UI();
	/**
	 * Verify the title of the page is correct.
	 * @param driver The driver to be used.
	 * @param expTitle Expected title text.
	 * @return if return nothing (""), means success; otherwise return "Failed: Logon"
	 * and this information will be output in verificationErrors;
	 */
    public String VerifyPageTitle(WebDriver driver,String expTitle) {
    	String pagetitle=driver.getTitle();
    	if(pagetitle.equals(expTitle)){
    		logger.info("Pass: page title is {"+ pagetitle+"}");
    		return "";}
    	else{
    		logger.error("Failed: page title is not {"+expTitle + "}, but is: {" + pagetitle+"}");
    		return "Failed: page tile"+"\r\n";
    	}
    }

	/**
	 * Select an option from a drop-down list
	 * @param webElement The drop-down list web element.
	 * @param theOptionToBeSelected The name of the option to be selected.
	 */
	public void selectOptionForDropdownList(WebElement webElement, String theOptionToBeSelected)
	{
		new Select(webElement).selectByVisibleText(theOptionToBeSelected);
	}
	
	/**
	 * Verify whether the "value" attribute of a WebElement matches the expectation. <br> 
	 * Example:<br>
	 * <i>private StringBuffer verificationErrors = new StringBuffer();<br>
	 * verificationErrors.append(UI.AssignmentPage.verifyElementValue(UI.AccountAssignment.Preparer_textField,"Preparer")));</i>
	 * @param webElement The web element which "value" attribute to be verified.
	 * @param expectedValue The expected text of the WebElement's "value" attribute.
	 * @return if return nothing (""), means success; otherwise return "Failed: Logon". And this information will be output in verificationErrors;
	 */
    public String verifyElementValue(WebElement webElement,String expectedValue) {
        if(webElement.getAttribute("value").equals(expectedValue)){
        	logger.info("Pass: "+webElement.getAttribute("value"));
        	return "";
        }else{
        	logger.error("Failed: "+webElement.getAttribute("value"));
        	return "\r\n"+"Failed! Expected value is: "+expectedValue+", Actual value is: "+webElement.getAttribute("value");
        }
    	
    }
    
	/**
	 * Verify the text of a web element matches the expectation. <br>
	 * Example:<br>
	 <i>private StringBuffer verificationErrors = new StringBuffer();<br>
	 * verificationErrors.append(UI.AccountsAdminGrid.verifyElementText(UI.AccountsAdminGrid.Entity_ofSearchedResult, "Entity"));
	 * @param webElement The web element which text to be verified.
	 * @param exptectedText The expected text of the web element.
	 * @return if return nothing (""), means success; otherwise return "Failed: Logon". And this information will be output in verificationErrors;
	 */
	public String verifyElementText(WebElement webElement,String exptectedText) {
        if(webElement.getText().equals(exptectedText)){
        	logger.info("Pass: "+webElement.getText());
        	return "";
        }else{
        	logger.error("Failed: "+webElement.getText());
        	return "\r\n"+"Failed! Expected text is: "+exptectedText+", Actual text is: "+webElement.getText();
        }
    }    
    
	/**
	 * Switch to newly popped up window.
	 * @param driver The WebDriver in use.<br>
	 * Sample:<br>
	 * UI.page.switchToNewWindow(driver)
	 */
	public boolean switchToNewWindow(WebDriver driver)
	{
		String currentWindow = driver.getWindowHandle();
		Set<String> handles = driver.getWindowHandles();
		Iterator<String> it = handles.iterator();
        while(it.hasNext()){
        	if (currentWindow == it.next()) 
        		{
        		continue;
        		}
        	WebDriver window = driver.switchTo().window(it.next());
        	logger.info("Title of current window: " + window.getTitle()+", URL is: " + window.getCurrentUrl());
        	return true;
        }
        return false;
	}

} 